package com.example.myapp;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapp.fragment.MainFragment;
import com.example.myapp.fragment.MessageFragment;
import com.example.myapp.fragment.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private MainFragment mainFragment;
    private MessageFragment messageFragment;
    private MineFragment mineFragment;

    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化控件
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        //设置点击事件
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId()==R.id.home){
                    selectedFragment(0);

                }else if(item.getItemId()==R.id.message){
                    selectedFragment(1);
                }else{
                    selectedFragment(2);
                }
                return true;
            }
        });


        //默认首页选中
        selectedFragment(0);

//        if (savedInstanceState == null) {
//            Fragment fragment = new com.example.myapp.fragment.MainFragment();
//            getSupportFragmentManager().beginTransaction()
//                    .replace(R.id.fragment_container, fragment)
//                    .commit();
//        }
    }

    private void selectedFragment(int position){

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);

        if(position == 0){
            if(mainFragment == null){
                mainFragment = new MainFragment();
                fragmentTransaction.add(R.id.fragment_container,mainFragment);
            }else {
                fragmentTransaction.show(mainFragment);
            }
        }else if(position == 1){
            if(messageFragment == null){
                messageFragment = new MessageFragment();
                fragmentTransaction.add(R.id.fragment_container,messageFragment);
            }else {
                fragmentTransaction.show(messageFragment);
            }
        }else {
            if(mineFragment == null){
                mineFragment = new MineFragment();
                fragmentTransaction.add(R.id.fragment_container,mineFragment);
            }else {
                fragmentTransaction.show(mineFragment);
            }
        }

        fragmentTransaction.commit();

    }

    private void hideFragment(FragmentTransaction fragmentTransaction){
        if(mainFragment!=null){
            fragmentTransaction.hide(mainFragment);
        }

        if(messageFragment!=null){
            fragmentTransaction.hide(messageFragment);
        }

        if(mineFragment!=null){
            fragmentTransaction.hide(mineFragment);
        }
    }
}
